package HelloSeries.ui.prefs.elems.flextables.view;

import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import HelloSeries.metier.Serie;
import HelloSeries.metier.Version;
import HelloSeries.ui.prefs.elems.flextables.editor.ButtonEditor;
import HelloSeries.ui.prefs.elems.flextables.editor.ComboBoxEditor;
import HelloSeries.ui.prefs.elems.flextables.editor.IntegerEditor;
import HelloSeries.ui.prefs.elems.flextables.editor.TextEditor;
import HelloSeries.ui.prefs.elems.flextables.model.FlexTableSerieModel;
import HelloSeries.ui.prefs.elems.flextables.renderer.ButtonRenderer;
import HelloSeries.ui.prefs.elems.flextables.renderer.DefaultSwingRenderer;
import HelloSeries.ui.prefs.elems.flextables.renderer.JComboBoxRenderer;

public class FlexTableSerie extends JPanel{

    private FlexTableSerieModel model;

    private JTable table;

    public FlexTableSerie( ArrayList<Serie> donnees )
    {
        super( new GridLayout( 1, 0 ) );
        String[] entete = { "Active", "Nom", "Saison", "Episode", "HD", "Langue", "Action" };
        this.model = new FlexTableSerieModel( entete, donnees );
        this.table = new JTable( model );
        setTableProperties( table );
        initRenderers( table );
        initColumnSizes();
        JScrollPane scrollPane = new JScrollPane( table );
        this.add( scrollPane );
    }

    private void setTableProperties( JTable table )
    {
        table.setShowGrid( false );
        table.setRowSelectionAllowed( false );
        table.setCellSelectionEnabled( false );
        table.setPreferredScrollableViewportSize( table.getPreferredSize() );
        table.setFocusable( false );
        table.setRowHeight( 25 );
        table.setBackground( null );
    }

    public void initRenderers( JTable table )
    {
        table.setDefaultRenderer( JTextField.class, new DefaultSwingRenderer() );
        table.setDefaultRenderer( JButton.class, new ButtonRenderer() );
        table.getColumn( "Action" ).setCellEditor( new ButtonEditor( new JCheckBox() ) );
        table.getColumn( "Nom" ).setCellEditor( new TextEditor() );
        table.getColumn( "Saison" ).setCellEditor( new IntegerEditor() );
        table.getColumn( "Episode" ).setCellEditor( new IntegerEditor() );
        table.getColumn( "Langue" ).setCellEditor( new ComboBoxEditor( Version.values() ) );
        table.getColumn( "Langue" ).setCellRenderer( new JComboBoxRenderer( Version.values() ) );

    }

    private void initColumnSizes()
    {
        FlexTableSerieModel model = (FlexTableSerieModel) table.getModel();
        TableColumn column = null;
        Component comp = null;
        int headerWidth = 0;
        int cellWidth = 0;
        TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
        if ( headerRenderer != null )
        {
            for ( int i = 0; i < model.getColumnCount(); i++ )
            {
                column = table.getColumnModel().getColumn( i );
                if ( column != null )
                {
                    comp = headerRenderer.getTableCellRendererComponent( table, column.getHeaderValue(), false, false, 0, 0 );
                    headerWidth = comp.getPreferredSize().width;
                    comp = table.getDefaultRenderer( model.getColumnClass( i ) ).getTableCellRendererComponent( table, model.getValueAt( 0, i ), false, false, 0, i );
                    cellWidth = comp.getPreferredSize().width;
                    column.setPreferredWidth( Math.max( headerWidth, cellWidth ) );
                }
            }

        }
    }

    public FlexTableSerieModel getModel()
    {
        return model;
    }

    public void setModel( FlexTableSerieModel model )
    {
        this.model = model;
    }

    public JTable getTable()
    {
        return table;
    }

    public void setTable( JTable table )
    {
        this.table = table;
    }

}
